package com.huike.pmps.utils;

import com.huike.pmps.model.constants.GlobalConstants;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
public class PmpUtil {

    /**
     * 将时间长度(时分秒)转换成 分钟
     *
     * @param hMS 时分秒 如：30:25:99
     * @return
     */
    public static Double timeToMinuteConvert(String hMS) {
        Double result = 0d;
        String[] fields = hMS.split("[:]");
        result += (Integer.valueOf(fields[0])) * 60;
        result += (Integer.valueOf(fields[1]));
        result += (Integer.valueOf(fields[2])) / 60;
        return result;
    }

    public static Double getScale(double doubleValue, int scale) {
        BigDecimal bigDecimal = new BigDecimal(doubleValue);
        return bigDecimal.setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }


    /**
     * 获取日期单位，比如：年，季度，月，周，天
     */
    public   static Integer getDateInfo(Date date, Integer dateType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        if (dateType.equals(GlobalConstants.YEAR)) {
            return calendar.get(Calendar.YEAR);
        } else if (dateType.equals(GlobalConstants.SEASON)) {
            //calendar.get(Calendar.MONTH)获取的到月份是0-11
            Integer month = calendar.get(Calendar.MONTH) + 1;
            if (month % 3 == 0) {
                return month / 3;
            } else {
                return month / 3 + 1;
            }
        } else if (dateType.equals(GlobalConstants.MONTH)) {
            return calendar.get(Calendar.MONTH) + 1;
        } else if (dateType.equals(GlobalConstants.WEEK_OF_YEAR)) {
            return calendar.get(Calendar.WEEK_OF_YEAR);
        } else if (dateType.equals(GlobalConstants.WEEK_OF_MONTH)) {
            return calendar.get(Calendar.WEEK_OF_MONTH);
        } else {
            return calendar.get(Calendar.DAY_OF_MONTH);
        }

    }
}
